package test_junit_shop.ProductStock;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import util.BaseDriver;

import java.util.List;

/**
 * Created by Eric on 2018/2/2.
 * 门店-采购列表
 */
public class PurchaseList extends BaseDriver{
    private static PurchaseList ns = new PurchaseList();
    public static PurchaseList getInstance(){
        return ns;
    }

    private PurchaseList() {
    }

    /**
     * 根据供应商，来退换相应数量的货
     * @param supplierName 供应商名
     * @param refundAmount 退货数量
     */
    public void refund(String supplierName, String refundAmount) throws InterruptedException {
        driver.switchTo().defaultContent();
        List<WebElement> frameList = driver.findElements(By.xpath("//iframe[@name='iframeundefined']"));
        driver.switchTo().frame(frameList.get(frameList.size() - 1));

        driver.findElement(By.xpath(String.format("//td[contains(text(), '%s')]/preceding-sibling::td[2]/input", supplierName))).click();
        driver.findElement(By.xpath("//button[@onclick='sales()']")).click();

        Thread.sleep(1000);
        driver.switchTo().frame(0);

        driver.findElement(By.xpath("//tbody/tr[1]/td[1]/input[@type='checkbox']")).click();
        WebElement refundAmountInput = driver.findElement(By.name("refundAmount"));
        new Actions(driver).moveToElement(refundAmountInput);

        refundAmountInput.clear();
        refundAmountInput.sendKeys(refundAmount);
        driver.findElement(By.xpath("//button[@onclick='StockOrderDetailCreate.setoff($(this))']")).click();
        Thread.sleep(2000);

        driver.findElement(By.xpath("//button[@onclick='StockOrderDetailCreate.setoff($(this))']/following-sibling::div[1]/table/tbody/tr")).click();
        driver.findElement(By.name("payAccountNo")).sendKeys("123456789");

        driver.switchTo().parentFrame();
        driver.findElement(By.linkText("确定")).click();

        Thread.sleep(1000);
        driver.findElements(By.linkText("确定")).get(1).click();

    }
}
